package com.oracle.malldemo.util;

import cn.hutool.core.lang.UUID;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.yungouos.pay.wxpay.WxPay;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class MyUtil {

    private static final String merchantId = "1611539741"; //微信商户ID
    private static final String payScretKey = "264B19DF8E2C4C95A8870B28D83D0BF7";

    /**
     * 大陆号码或香港号码均可
     */
    public static boolean isPhoneLegal(String str)throws PatternSyntaxException {
        return isChinaPhoneLegal(str) || isHKPhoneLegal(str);
    }

    /**
     * 大陆手机号码11位数，匹配格式：前三位固定格式+后8位任意数
     * 此方法中前三位格式有：
     * 13+任意数
     * 15+除4的任意数
     * 18+除1和4的任意数
     * 17+除9的任意数
     * 147
     */
    public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
        String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 香港手机号码8位数，5|6|8|9开头+7位任意数
     */
    public static boolean isHKPhoneLegal(String str)throws PatternSyntaxException {
        String regExp = "^(5|6|8|9)\\d{7}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }


    public static boolean isEmail(String emails){
        String reg = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(emails);
        return m.matches();
    }

    public static String encryPassword(String password){
        //String content = "123456";
        //随机生成密钥
        byte[] key = "helloworld hello".getBytes();
        //构建
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
        //加密为16进制表示
        String encryptHex = aes.encryptHex(password);
        return encryptHex;
    }

    public static String dateToChineseDateFormat(Date date){
        return new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(date);
    }

    public static String dataToSimpleChineseDateFormat(Date date){
        return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
    }

    //生成订单编号的方法
    public static String generateOrderId(){
        //UUID
        return  dataToSimpleChineseDateFormat(new Date())+UUID.fastUUID().toString().replaceAll("-","");
    }

    public static void main(String[] args) {

        //System.out.println(MyUtil.generateOrderId());
        //String codeImgUrl =  MyUtil.weipay();
        //System.out.println(codeImgUrl);
    }

    public static String weipay(String oid,Double totalPrice,String itemName){
        String result= WxPay.nativePay(oid, totalPrice+"", merchantId, itemName, null, null, null, null,null,null,null,null,payScretKey);
        return result;
    }

}
